package com.etime.shycourse.services.coupons.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.etime.shycourse.config.Tools;
import com.etime.shycourse.services.coupons.interfaces.CouponactivityServices;
import com.etime.shycourse.dao.CouponactivityMapper;
import com.etime.shycourse.dto.CurrentUser;
import com.etime.shycourse.pojo.Couponactivity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 *
 * @author wb
 * @since 2022-03-15
 */
@Service
public class CouponactivityServiceImpl implements CouponactivityServices {
    @Autowired
    private Tools tools;

    @Autowired
    private CouponactivityMapper mapper;

    @Override
    public Integer insertActivity(Couponactivity couponactivity, String token) {
        CurrentUser sysUser = tools.getCurrentUserFromRedis (token);
        // 判断用户存在
        if (sysUser == null || sysUser.getUser () == null) {
            return 0;
        }
        couponactivity.setActiveuserid (sysUser.getUser ().getUserid ());
        //创建人姓名
        String userName = null;
        if (sysUser.getUser ().getUsertruename () != null) {
            userName = sysUser.getUser ().getUsertruename ();
        }
        if (sysUser.getUser ().getNickname () != null) {
            userName = userName + "(" + sysUser.getUser ().getNickname () + ")";
        }
        // 设置活动创建人姓名
        couponactivity.setActiveusername (userName);
        // 1-创建未发布（活动不生效）-创建时默认状态
        couponactivity.setActivestate (1);
        // 活动创建时间
        couponactivity.setActivecreatetime (new Date ());
        return mapper.insert (couponactivity);
    }

    @Override
    public Integer updateActivityById(Couponactivity couponactivity, String token) {
        CurrentUser sysUser = tools.getCurrentUserFromRedis (token);
        // 判断用户存在
        if (sysUser == null || sysUser.getUser () == null) {
            return 0;
        }
        couponactivity.setActiveupdateuserid (sysUser.getUser ().getUserid ());
        //修改姓名
        String userName = null;
        if (sysUser.getUser ().getUsertruename () != null) {
            userName = sysUser.getUser ().getUsertruename ();
        }
        if (sysUser.getUser ().getNickname () != null) {
            userName = userName + "(" + sysUser.getUser ().getNickname () + ")";
        }
        // 设置活动修改人姓名
        couponactivity.setActiveupdateusername (userName);
        // 活动最后修改时间
        couponactivity.setActiveupdatetime (new Date ());
        return mapper.updateById (couponactivity);
    }

    /**
     * 修改活动的状态
     *
     * @param couponactivity
     * @return 1 成功 0失败
     */
    @Override
    public Integer updateActivityById(Couponactivity couponactivity) {
        return mapper.updateById (couponactivity);
    }

    /**
     * 活动的分页
     *
     * @param couponactivity
     * @return Page<Couponactivity>
     */
    @Override
    public Page<Couponactivity> selectActivityPage(Couponactivity couponactivity, Long pageNum, Long pageSize) {
        Page<Couponactivity> page;
        if (pageNum != null & pageSize != null) {
            page = new Page<> (pageNum, pageSize);
        } else {
            page = new Page<> ();
        }
        QueryWrapper<Couponactivity> wrapper = new QueryWrapper<> ();
        // 根据活动状态
        if (couponactivity.getActivestate () != null) {
            wrapper.eq ("activestate", couponactivity.getActivestate ());
        }
        //活動類型
        if (couponactivity.getActivetypeid () != null) {
            wrapper.eq ("activetypeid", couponactivity.getActivetypeid ());
        }
        // 根据活动标题  activetitle
        if (couponactivity.getActivetitle () != null) {
            wrapper.like ("activetitle", couponactivity.getActivetitle ());
        }
        // 活动是否允许转发
        if (couponactivity.getActiveallowdforward () != null) {
            wrapper.eq ("activeallowdforward", couponactivity.getActiveallowdforward ());
        }
        // 自动发放or手动领取
        if (couponactivity.getActiveway () != null) {
            wrapper.eq ("activeway", couponactivity.getActiveway ());
        }
        // 活动状态
        wrapper.orderByAsc ("activestate");
        // 创建时间
        wrapper.orderByDesc ("activecreatetime");
        return mapper.selectPage (page, wrapper);
    }

    @Override
    public Couponactivity selectById(Integer Id) {
        return mapper.selectById (Id);
    }
}
